/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package adcomputer;

import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.logging.Level;
import java.util.logging.Logger;

/**
 *
 * @author Edgar
 */
public class RegistrarVenta extends javax.swing.JPanel {

  ArrayList<Integer>nj;
  ArrayList<Integer>cl;
  ArrayList<Integer>nc;
    public RegistrarVenta() {
        initComponents();
    }
    public void  Busqueda()
    {
        nj=new ArrayList<>();
        nc= new ArrayList<>();
     
            bajaj.setVisible(true);
           
         try {
       BaseDeDatos bd= new BaseDeDatos();
                bd.conectar();
                String query1="Select nombre, idDatosjuego,Existencia from DatosJuego";
                       ResultSet rs=bd.consultar(query1);
                while(rs.next())
                {
                    System.out.println(rs.getString(1));
                   bajaj.addItem(rs.getString(1));  
                   nj.add(rs.getInt(2));
                   nc.add(rs.getInt(3));
                    
                }
        } catch (SQLException ex) {
            Logger.getLogger(AñadirCompra.class.getName()).log(Level.SEVERE, null, ex);
        }
    }
    public void  BusquedaPerrona()
    {
        cl=new ArrayList<>();
     
         try {
       BaseDeDatos bd= new BaseDeDatos();
                bd.conectar();
                String query1="Select nombre, idclientes from clientes";
                       ResultSet rs=bd.consultar(query1);
                while(rs.next())
                {
                    System.out.println(rs.getString(1));
                   clienteb.addItem(rs.getString(1));  
                   cl.add(rs.getInt(2));
                    
                }
        } catch (SQLException ex) {
            Logger.getLogger(AñadirCompra.class.getName()).log(Level.SEVERE, null, ex);
        }
    }

    /**
     * This method is called from within the constructor to initialize the form.
     * WARNING: Do NOT modify this code. The content of this method is always
     * regenerated by the Form Editor.
     */
    @SuppressWarnings("unchecked")
    // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
    private void initComponents() {

        jLabel1 = new javax.swing.JLabel();
        bajaj = new javax.swing.JComboBox();
        jLabel2 = new javax.swing.JLabel();
        numcopias = new javax.swing.JTextField();
        jButton1 = new javax.swing.JButton();
        jLabel3 = new javax.swing.JLabel();
        clienteb = new javax.swing.JComboBox();

        jLabel1.setText("Juego");

        bajaj.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                bajajActionPerformed(evt);
            }
        });

        jLabel2.setText("NumCopias");

        jButton1.setText("Guardar");
        jButton1.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jButton1ActionPerformed(evt);
            }
        });

        jLabel3.setText("Cliente");

        javax.swing.GroupLayout layout = new javax.swing.GroupLayout(this);
        this.setLayout(layout);
        layout.setHorizontalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addGroup(layout.createSequentialGroup()
                        .addContainerGap()
                        .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                            .addComponent(jLabel2)
                            .addComponent(jLabel1))
                        .addGap(25, 25, 25)
                        .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                            .addComponent(bajaj, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                            .addComponent(numcopias, javax.swing.GroupLayout.PREFERRED_SIZE, 99, javax.swing.GroupLayout.PREFERRED_SIZE)))
                    .addGroup(layout.createSequentialGroup()
                        .addGap(6, 6, 6)
                        .addComponent(jLabel3, javax.swing.GroupLayout.PREFERRED_SIZE, 44, javax.swing.GroupLayout.PREFERRED_SIZE)
                        .addGap(18, 18, 18)
                        .addComponent(clienteb, javax.swing.GroupLayout.PREFERRED_SIZE, 70, javax.swing.GroupLayout.PREFERRED_SIZE))
                    .addGroup(layout.createSequentialGroup()
                        .addGap(76, 76, 76)
                        .addComponent(jButton1)))
                .addContainerGap(213, Short.MAX_VALUE))
        );
        layout.setVerticalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addGap(26, 26, 26)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.TRAILING)
                    .addGroup(layout.createSequentialGroup()
                        .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                            .addComponent(jLabel1)
                            .addComponent(bajaj, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                        .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                            .addComponent(jLabel2)
                            .addComponent(numcopias, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                        .addGap(18, 18, 18)
                        .addComponent(clienteb, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                    .addComponent(jLabel3))
                .addGap(18, 18, 18)
                .addComponent(jButton1)
                .addContainerGap(144, Short.MAX_VALUE))
        );
    }// </editor-fold>//GEN-END:initComponents

    private void bajajActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_bajajActionPerformed
        // TODO add your handling code here:
    }//GEN-LAST:event_bajajActionPerformed

    private void jButton1ActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jButton1ActionPerformed
String nombre=(String)bajaj.getSelectedItem();
int numerocopias=Integer.parseInt(numcopias.getText());
int in=bajaj.getSelectedIndex();
int clIn=clienteb.getSelectedIndex();
 BaseDeDatos bd= new BaseDeDatos();
            bd.conectar();                    
   String query="Insert into Ventas values(null," + nj.get(in)+"," + cl.get(clIn) + "," + numerocopias+ ")";
            bd.modificar(query);
            query="Update DatosJuego set Existencia = "+ (nc.get(in)-numerocopias)+ " where idDatosjuego="+ nj.get(in);
            bd.modificar(query);
            bd.desconectar(); 

    }//GEN-LAST:event_jButton1ActionPerformed

    // Variables declaration - do not modify//GEN-BEGIN:variables
    private javax.swing.JComboBox bajaj;
    private javax.swing.JComboBox clienteb;
    private javax.swing.JButton jButton1;
    private javax.swing.JLabel jLabel1;
    private javax.swing.JLabel jLabel2;
    private javax.swing.JLabel jLabel3;
    private javax.swing.JTextField numcopias;
    // End of variables declaration//GEN-END:variables
}
